const compose = (...fns) => (x) => fns.reduceRight((arg, f) => f(arg), x)
// compose(f,g,p)(x)  ===  f(g(p(x)))


const map = fn => arr => arr.map(fn)

const double = x => x * 2

const array = [1, 2, 3]

const doubleArray = map(double)


console.log(doubleArray(array));


const users = [
  {
    role: 'user',
    email: 'user@gmail.com'
  },
  {
    role: 'admin',
    email: 'admin@gmail.com'
  },

  {
    role: 'admin',
    email: 'otheradmin@gmail.com'
  }
]


const getAdminEmails = users =>
  users
    .filter(u => u.role === 'admin')
    .map(u => u.email);


const prop = key => obj => obj[key]
const propEqTo = v => key => obj => obj[key] === v
const filter = f => array => array.filter(f)


const getEMail = map(prop("email"))

const getAdmin = filter(propEqTo("admin")("role"))

const getAdminEmails2 = compose(getEMail, getAdmin)


console.log(getAdminEmails(users));
console.log(getAdminEmails2(users));


const select = from => to => array => array.slice(from, to)
select(6)(9)([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) // [6,7,8]



const compose = (...fns) => (x) => fns.reduceRight((arg, f) => f(arg), x)

const selectFrom = from => array => array.slice(from)
const selectTo = to => array => array.slice(0, to)

const selectFrom6To9 = compose(
  selectFrom(6),
  selectTo(9)
)

selectFrom6To9([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) // [6,7,8]
